<?php

class Core_Component_Account_Controller_Backend_Group {
    
    public function __construct() {
        $GLOBALS[RC]->getHelper()->setLocation('{account.account}', '{account.group_index}');
    }
    
    public function mappingAction(){
        return array(
            'create'=>'edit'
        );
    }    

    public function indexAction($pageindex = 1, $pagesize = 20) {
        $q = App::getModel('account/group')->getQuery();
        $list = $q->pagination($pageindex, $pagesize)->fetch();
        $count = $q->fetchCount();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'record_count' => $count));
    }

    public function editAction($id = NULL) {
        $h = $GLOBALS[RC]->getHelper();
        $model = App::getModel('account/group');
        if (empty($id)) {
            $h->setLocation('{account.group_create}');
        } else {
            $model->load($id)
                  ->setName($GLOBALS[RC]->getLanguage()->__($model->getName()))
                  ->setDescription($GLOBALS[RC]->getLanguage()->__($model->getDescription()));
            $h->setLocation($model->getName());
        }
        return $GLOBALS[RC]->getHelper()->modelFormResult(
                $model,
                array(),
                '*/*/edit');
    }

    public function savePostAction(Core_Component_Account_Model_Group $model) {
        try {
            $model->merge()->validate()->save();
            $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
            return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');
        } catch (Core_Component_Account_Exception $ex) {
            if ($ex->getMessage() == Core_Component_Account_Exception::GROUP_REPEATED) {
                $GLOBALS[RC]->getMessager()->addError('{account.'.$ex->getMessage().'}');
            }
            return $GLOBALS[RC]->getHelper()->modelFormResult(
                    $model,
                    array(),
                    '*/*/edit');
        }
    }

    public function deleteAction($ids) {
        App::getModel('account/group')->deleteBatch($ids);
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');
    }    
}